<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是STM32开发——GPIO外设基础的一些的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV16-12-GPIO-01-STM32的GPIO">
<meta property="og:url" content="https://sumumm.github.io/post/63f07cb0.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是STM32开发——GPIO外设基础的一些的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230430192413234.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416171441624.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416172714613.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230501201907897.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image229.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230501203914339.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image324.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image419.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416173458031.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416175828910.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416175929792.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416175951176.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416180036946.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416180111135.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230501203914339.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416180131497.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230506192959951.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416180215294.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/20180409133536882.jpeg">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416190146605.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191434797.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191434797.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191400174.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416190146605.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230506192914603.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416214223104.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191400174.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191130706.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230506213749244.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230506214456939.png">
<meta property="article:published_time" content="2023-06-04T14:06:51.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.009Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV16-STM32开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230430192413234.png">


<link rel="canonical" href="https://sumumm.github.io/post/63f07cb0.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/63f07cb0.html","path":"post/63f07cb0.html","title":"LV16-12-GPIO-01-STM32的GPIO"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV16-12-GPIO-01-STM32的GPIO | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81GPIO%E7%AE%80%E4%BB%8B"><span class="nav-text">一、GPIO简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-GPIO%E6%A1%86%E5%9B%BE"><span class="nav-text">1. GPIO框图</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%BF%9D%E6%8A%A4%E4%BA%8C%E6%9E%81%E7%AE%A1"><span class="nav-text">1.1 保护二极管</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E4%B8%8A%E4%B8%8B%E6%8B%89%E7%94%B5%E9%98%BB"><span class="nav-text">1.2上下拉电阻</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-P-MOS%E7%AE%A1%E5%92%8CN-MOS%E7%AE%A1"><span class="nav-text">1.3 P-MOS管和N-MOS管</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-1-%E6%8E%A8%E6%8C%BD%E8%BE%93%E5%87%BA"><span class="nav-text">1.3.1 推挽输出</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-3-2-%E5%BC%80%E6%BC%8F%E8%BE%93%E5%87%BA"><span class="nav-text">1.3.2 开漏输出</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-4-%E5%BA%94%E7%94%A8"><span class="nav-text">1.4 应用</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E8%BE%93%E5%87%BA%E6%95%B0%E6%8D%AE%E5%AF%84%E5%AD%98%E5%99%A8"><span class="nav-text">1.4 输出数据寄存器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-5-%E5%A4%8D%E7%94%A8%E5%8A%9F%E8%83%BD%E8%BE%93%E5%87%BA"><span class="nav-text">1.5 复用功能输出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-6-%E8%BE%93%E5%85%A5%E6%95%B0%E6%8D%AE%E5%AF%84%E5%AD%98%E5%99%A8"><span class="nav-text">1.6 输入数据寄存器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-7-%E5%A4%8D%E7%94%A8%E5%8A%9F%E8%83%BD%E8%BE%93%E5%85%A5"><span class="nav-text">1.7 复用功能输入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-8-%E6%A8%A1%E6%8B%9F%E8%BE%93%E5%85%A5%E8%BE%93%E5%87%BA"><span class="nav-text">1.8 模拟输入输出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-9-TTL%E8%82%96%E7%89%B9%E5%9F%BA%E8%A7%A6%E5%8F%91%E5%99%A8"><span class="nav-text">1.9 TTL肖特基触发器</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-GPIO%E7%9A%84%E5%85%AB%E7%A7%8D%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F"><span class="nav-text">2. GPIO的八种工作模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E6%B5%AE%E7%A9%BA%E8%BE%93%E5%85%A5%E6%A8%A1%E5%BC%8F"><span class="nav-text">2.1 浮空输入模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%B8%8A%E6%8B%89%E8%BE%93%E5%85%A5%E6%A8%A1%E5%BC%8F"><span class="nav-text">2.2 上拉输入模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E4%B8%8B%E6%8B%89%E8%BE%93%E5%85%A5%E6%A8%A1%E5%BC%8F"><span class="nav-text">2.3 下拉输入模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-%E6%A8%A1%E6%8B%9F%E8%BE%93%E5%85%A5%E6%A8%A1%E5%BC%8F"><span class="nav-text">2.4 模拟输入模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-5-%E5%BC%80%E6%BC%8F%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F"><span class="nav-text">2.5 开漏输出模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-6-%E5%A4%8D%E7%94%A8%E5%8A%9F%E8%83%BD%E5%BC%80%E6%BC%8F%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F"><span class="nav-text">2.6 复用功能开漏输出模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-7-%E6%8E%A8%E6%8C%BD%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F"><span class="nav-text">2.7 推挽输出模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-8-%E5%A4%8D%E7%94%A8%E5%8A%9F%E8%83%BD%E6%8E%A8%E6%8C%BD%E8%BE%93%E5%87%BA%E6%A8%A1%E5%BC%8F"><span class="nav-text">2.8 复用功能推挽输出模式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-GPIO-%E8%BE%93%E5%87%BA%E9%80%9F%E5%BA%A6"><span class="nav-text">3. GPIO 输出速度  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%87%A0%E4%B8%AA%E9%97%AE%E9%A2%98"><span class="nav-text">4. 几个问题</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E4%BB%80%E4%B9%88%E6%98%AF%E6%8E%A8%E6%8C%BD%E7%BB%93%E6%9E%84%E5%92%8C%E6%8E%A8%E6%8C%BD%E7%94%B5%E8%B7%AF"><span class="nav-text">4.1 什么是推挽结构和推挽电路</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E5%BC%80%E6%BC%8F%E8%BE%93%E5%87%BA%E5%92%8C%E6%8E%A8%E6%8C%BD%E8%BE%93%E5%87%BA%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-text">4.2 开漏输出和推挽输出的区别</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%AF%84%E5%AD%98%E5%99%A8%E8%AF%B4%E6%98%8E"><span class="nav-text">二、寄存器说明</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81STM32%E7%AB%AF%E5%8F%A3%E5%A4%8D%E7%94%A8%E5%92%8C%E9%87%8D%E6%98%A0%E5%B0%84"><span class="nav-text">三、STM32端口复用和重映射</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%AB%AF%E5%8F%A3%E5%A4%8D%E7%94%A8"><span class="nav-text">1. 端口复用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-text">1.1 基本概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E7%AB%AF%E5%8F%A3%E5%A4%8D%E7%94%A8%E9%85%8D%E7%BD%AE%E6%AD%A5%E9%AA%A4"><span class="nav-text">1.2 端口复用配置步骤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E8%B5%84%E6%96%99%E6%9F%A5%E6%89%BE"><span class="nav-text">1.3 资料查找</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%AB%AF%E5%8F%A3%E9%87%8D%E6%98%A0%E5%B0%84"><span class="nav-text">2. 端口重映射</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-text">2.1 基本概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E9%83%A8%E5%88%86%E9%87%8D%E6%98%A0%E5%B0%84-%E5%AE%8C%E5%85%A8%E9%87%8D%E6%98%A0%E5%B0%84"><span class="nav-text">2.2 部分重映射 &amp; 完全重映射</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E9%87%8D%E6%98%A0%E5%B0%84%E6%AD%A5%E9%AA%A4"><span class="nav-text">2.3 重映射步骤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-4-%E8%B5%84%E6%96%99%E6%9F%A5%E6%89%BE"><span class="nav-text">2.4 资料查找</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E4%BD%8D%E5%B8%A6%E6%93%8D%E4%BD%9C"><span class="nav-text">四、位带操作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86%E5%9B%9E%E9%A1%BE"><span class="nav-text">1. 相关知识回顾</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%9B%B8%E5%85%B3%E5%AE%8F%E5%AE%9A%E4%B9%89"><span class="nav-text">2. 相关宏定义</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%AE%8F"><span class="nav-text">2.1 宏</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%88%86%E6%9E%90"><span class="nav-text">2.2 分析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3. 使用实例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81HAL%E5%BA%93%E5%87%BD%E6%95%B0%E5%88%86%E6%9E%90"><span class="nav-text">五、HAL库函数分析</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-GPIO-InitTypeDef"><span class="nav-text">1. GPIO_InitTypeDef</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%AF%BB%E5%86%99%E7%9B%B8%E5%85%B3%E5%87%BD%E6%95%B0"><span class="nav-text">2. 读写相关函数</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/63f07cb0.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV16-12-GPIO-01-STM32的GPIO | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV16-12-GPIO-01-STM32的GPIO
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-06-04 22:06:51" itemprop="dateCreated datePublished" datetime="2023-06-04T22:06:51+08:00">2023-06-04</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV16-STM32开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>12k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>44 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是STM32开发——GPIO外设基础的一些的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <ul><li>通用</li></ul><table><tr><td align="center">分类  </td><td align="center">网址</td><td align="center">说明</td></tr><tr><td align="center" rowspan="4">官方网站</td><td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td><td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td></tr><tr>                                            <td align="left"><a href="https://www.st.com/content/st_com/zh.html" target="_blank">https://www.st.com/content/st_com/zh.html</a></td><td align="left">ST官方网站，在这里我们可以找到STM32的相关文档</td></tr><tr>                                            <td align="left"><a href="https://www.stmcu.com.cn/" target="_blank">https://www.stmcu.com.cn/</a></td><td align="left">意法半导体ST中文官方网站，在这里我们可以找到STM32的相关中文参考文档</td></tr><tr>                                            <td align="left"><a href="http://elm-chan.org/fsw/ff/00index_e.html" target="_blank">http://elm-chan.org/fsw/ff/00index_e.html</a></td><td align="left">FatFs文件系统官网</td></tr><tr><td align="center" rowspan="3">教程书籍</td><td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M3权威指南(中文).pdf" target="_blank">《ARM Cortex-M3权威指南》</a></td><td alirn="left" rowspan="3">ARM公司专家Joseph Yiu（姚文祥）的力作，中文翻译是NXP的宋岩</td></tr><tr>                                            <td align="left"><a href="STM32开发相关资料/01ARM参考资料/ARM Cortex-M0权威指南(中文).pdf" target="_blank">《ARM Cortex-M0权威指南》</a></td></tr><tr>                                            <td align="left"><a href="" target="_blank">《ARM Cortex-M3与Cortex-M4权威指南》</a></td></tr><tr><td align="center" rowspan="4">开发论坛</td><td align="left"><a href="http://47.111.11.73/forum.php" target="_blank">http://47.111.11.73/forum.php</a></td><td align="left">开源电子网，正点原子的资料下载及问题讨论论坛</td></tr><tr>                                            <td align="left"><a href="https://www.firebbs.cn/forum.php" target="_blank">https://www.firebbs.cn/forum.php</a></td><td align="left">国内Kinetis开发板-野火/秉火（刘火良）主持的论坛，现也做STM32和i.MX RT</td></tr><tr>                                            <td align="left"><a href="https://www.amobbs.com/index.php" target="_blank">https://www.amobbs.com/index.php</a></td><td align="left">阿莫（莫进明）主持的论坛，号称国内最早最火的电子论坛，以交流Atmel AVR系列单片机起家，现已拓展到嵌入式全平台，其STM32系列帖子有70W+。</td></tr><tr>                                            <td align="left"><a href="http://download.100ask.net/index.html" target="_blank">http://download.100ask.net/index.html</a></td><td align="left">韦东山嵌入式资料中心，有些STM32和linux的相关资料也可以来这里找。</td></tr><tr><td align="center" rowspan="3">博客参考</td><td align="left"><a href="http://www.openedv.com/" target="_blank">http://www.openedv.com/</a></td><td align="left">开源网-原子哥个人博客</td></tr><tr>                                            <td align="left"><a href="http://blog.chinaaet.com/jihceng0622" target="_blank">http://blog.chinaaet.com/jihceng0622</a></td><td align="left">博主是原Freescale现NXP的现场应用工程师</td></tr><tr>                                            <td align="left"><a href="https://community.arm.com/arm-community-blogs/b/architectures-and-processors-blog/posts/cortex-m-resources" target="_blank">cortex-m-resources</a></td><td align="left">这其实并不算是一个博客，这是ARM公司专家Joseph Yiu收集整理的所有对开发者有用的官方Cortex-M资料链接（也包含极少数外部资源链接）</td></tr></table><ul><li>STM32</li></ul><table>    <tr><td align="center" rowspan="2">STM32</td><td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103/hal_general/zh/latest/index.html" target="_blank">STM32 HAL库开发实战指南——基于F103系列开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr><tr>                                            <td align="left"><a href="https://doc.embedfire.com/mcu/stm32/f103badao/std/zh/latest/index.html" target="_blank">STM32库开发实战指南——基于野火霸道开发板</a></td><td align="left">野火STM32开发教程在线文档</td></tr></table><ul><li>SD卡</li></ul><table>    <tr><td align="left"><a href="https://www.sdcard.org/" target="_blank">SD Association</a></td><td align="left">提供了SD存储卡和SDIO卡系统规范</td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr><td align="left"><a href="https://www.st.com/resource/en/datasheet/stm32f103ze.pdf" target="_blank">STM32F103xx英文数据手册</a></td><td align="left">STM32F103xC/D/E系列的英文数据手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/709978" target="_blank">STM32F103xx中文数据手册</a></td><td align="left">STM32F103xC/D/E系列的中文数据手册</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf" target="_blank">STM32F10xxx英文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的英文参考手册</td></tr>    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/710001" target="_blank">STM32F10xxx中文参考手册（RM0008）</a></td><td align="left">STM32F10xxx系列的中文参考手册</td></tr>    <tr><td align="left"><a href="https://developer.arm.com/documentation/100165/0201/?lang=en" target="_blank">Arm Cortex-M3 处理器技术参考手册-英文版</a></td><td align="left">Cortex-M3技术参考手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf" target="_blank">STM32F10xxx Cortex-M3编程手册-英文版(PM0056)</a></td><td align="left">STM32F10xxx/20xxx/21xxx/L1xxxx系列Cortex-M3编程手册-英文版</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/" target="_blank">SD卡相关资料——最新版本</a></td><td align="left">有关SD卡的一些资料可以从这里下载</td></tr>    <tr><td align="left"><a href="https://www.sdcard.org/downloads/pls/archives/" target="_blank">SD卡相关资料——历史版本</a></td><td align="left">有关SD卡的一些历史版本资料可以从这里下载，比如后边看的SD卡2.0协议</td></tr>    <tr><td align="left"><a href="./" target="_blank">SD 2.0 协议标准完整版</a></td><td align="left">这是一篇关于SD卡2.0协议的中文文档，还是比较有参考价值的，可以一看</td></tr></table>
              </div>
            </details> 

<h1 id="一、GPIO简介"><a href="#一、GPIO简介" class="headerlink" title="一、GPIO简介"></a><font size=3>一、GPIO简介</font></h1><p>GPIO是通用输入输出端口的简称，简单来说就是STM32可控制的引脚，STM32芯片的GPIO引脚与外部设备连接起来，从而实现与外部通讯、控制以及数据采集的功能。 STM32芯片的GPIO被分成很多组，每组有16个引脚（也有可能少于16个），如型号为STM32F103ZET6型号的芯片有GPIOA、GPIOB、GPIOC至GPIOG共7组GPIO，一共就有16x7&#x3D;112个IO，该芯片一共144个引脚，其中GPIO引脚就占了一大部分，所有的GPIO引脚都有基本的输入输出功能。</p>
<p>最基本的输出功能是由STM32控制引脚输出高、低电平，实现开关控制，如把GPIO引脚接入到LED灯，那就可以控制LED灯的亮灭，引脚接入到继电器或三极管，那就可以通过继电器或三极管控制外部大功率电路的通断。</p>
<p>最基本的输入功能是检测外部输入电平，如把GPIO引脚连接到按键，通过电平高低区分按键是否被按下。</p>
<h2 id="1-GPIO框图"><a href="#1-GPIO框图" class="headerlink" title="1. GPIO框图"></a><font size=3>1. GPIO框图</font></h2><p>在《STM32F10xx中文参考手册》的 8.1 GPIO功能描述 中就有GPIO的框图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230430192413234.png" alt="image-20230430192413234" style="zoom:33%;" />

<p>通过GPIO硬件结构框图，就可以从整体上深入了解GPIO外设及它的各种应用模式。该图从最右端看起，最右端就是代表STM32芯片引出的GPIO引脚，其余部件都位于芯片内部。</p>
<p>这里需要注意的是，在查看《STM32中文参考手册V10》中的GPIO的表格时，会看到有“FT”一列，这代表着这个GPIO口时兼容3.3V和5V的；如果没有标注“FT”，就代表着不兼容5V。然后VDD和VSS是多少？我们需要查阅一下 <a target="_blank" rel="noopener" href="https://www.st.com/resource/en/datasheet/stm32f103ze.pdf">STM32数据手册</a> 的 2.3.11 Power supply schemes可以知道VDD &#x3D; 2.0 to 3.6 V，VSS的话其实是接到了GND，作为公共地。</p>
<h3 id="1-1-保护二极管"><a href="#1-1-保护二极管" class="headerlink" title="1.1 保护二极管"></a><font size=3>1.1 保护二极管</font></h3><p>我们来看一下序号①，序号①是两个保护二极管：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416171441624.png" alt="image-20230416171441624" style="zoom:50%;" />

<p>引脚的两个保护二级管可以防止引脚外部过高或过低的电压输入：当引脚电压高于VDD时， 上方的二极管导通；当引脚电压低于VSS时，下方的二极管导通，防止不正常电压引入芯片导致芯片烧毁。</p>
<p>尽管有这样的保护，并不意味着STM32的引脚能直接外接大功率驱动器件，如直接驱动电机，强制驱动要么电机不转，要么导致芯片烧坏，必须要加大功率及隔离电路驱动。</p>
<h3 id="1-2上下拉电阻"><a href="#1-2上下拉电阻" class="headerlink" title="1.2上下拉电阻"></a><font size=3>1.2上下拉电阻</font></h3><p>我们来看一下序号②：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416172714613.png" alt="image-20230416172714613" style="zoom:50%;" />

<p>可以使管脚默认为高电平VDD或者低电平VSS，不配置的时候表示管脚浮空，电平状态未知。</p>
<h3 id="1-3-P-MOS管和N-MOS管"><a href="#1-3-P-MOS管和N-MOS管" class="headerlink" title="1.3 P-MOS管和N-MOS管"></a><font size=3>1.3 P-MOS管和N-MOS管</font></h3><p>我们再来看一下序号③的部分：GPIO引脚线路经过两个保护二极管后，向上流向“输入模式”结构，向下流向“输出模式”结构。先看输出模式部分，线路经过一个由P-MOS和N-MOS管组成的单元电路。这个结构使GPIO具有了“<strong>推挽输出</strong>”和“<strong>开漏输出</strong>”两种模式。</p>
<h4 id="1-3-1-推挽输出"><a href="#1-3-1-推挽输出" class="headerlink" title="1.3.1 推挽输出"></a><font size=3>1.3.1 推挽输出</font></h4><p>所谓的<strong>推挽输出模式</strong>，是根据这两个MOS管的工作方式来命名的<strong>。推挽结构由两个MOS管按互补对称的方式连接，任意时刻总是其中一个三极管导通，另一个三极管截止。</strong>   内部由一个P-MOS管和一个N-MOS管组成，两个MOS管的栅极（ Gate， G）接到了左侧“输出控制” ，漏极（ Drain， D）接到外部输出， P-MOS管的源极（ Source， S）接到VDD（ 3.3V） ，N-MOS管的源极接到Vss（ 0V）。</p>
<p><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230501201907897.png" alt="image-20230501201907897"></p>
<ul>
<li>**对PMOS来说，Vs-Vg &gt; Vgs(th)**，即S极和G极的压差大于一定值，PMOS管会导通。“ 输出控制 ”向两个MOS管栅极加一定电压时，P-MOS导通，此时VDD 经过P-MOS管的S&rarr;G&rarr;D输出， N-MOS管处于高阻态（电阻很大，近似开路） ，整体对外为高电平。（开启电压和其他参数可以看具体器件的SPEC）</li>
<li>**对NMOS来说，Vg-Vs &gt; Vgs(th)**，即G极和S极的压差大于一定值，NMOS管会导通，但是也不能大太多，否则烧坏MOS管。“输出控制” 取消向两个MOS管栅极施加电压，N-MOS管源极和漏极导通，此时Vss 经过N-MOS管的S&rarr;G&rarr;D输出，整体对外为低电平。 P-MOS管源极和栅极截止， P-MOS管处于高阻态。（开启电压和其他参数可以看具体器件的SPEC）</li>
</ul>
<p>推挽模式下，让“输出控制” 变为了VDD&#x2F;Vss输出， 使得输出电流增大， 提高了输出引脚的驱动能力，提高了电路的负载能力和开关的动作速度。  </p>
<p>推挽输出模式时的等效电路如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image229.png" alt="推挽等效电路" style="zoom:50%;" />

<p>在该结构中输入高电平时，经过反向后，上方的P-MOS导通，下方的N-MOS关闭， 对外输出高电平；而在该结构中输入低电平时，经过反向后，N-MOS管导通，P-MOS关闭，对外输出低电平。当引脚高低电平切换时， 两个管子轮流导通，P管负责灌电流，N管负责拉电流，使其负载能力和开关速度都比普通的方式有很大的提高。</p>
<h4 id="1-3-2-开漏输出"><a href="#1-3-2-开漏输出" class="headerlink" title="1.3.2 开漏输出"></a><font size=3>1.3.2 开漏输出</font></h4><p>开漏模式下， “输出控制”不会控制P-MOS管， “输出控制” <strong>只会向N-MOS管栅极加一定电压</strong>， 两个MOS管都处于截止状态， 两个漏极处于悬空状态，称之为漏极开路。 “输出控制” 取消栅极的施加电压， PMOS管依旧处于高阻态， N-MOS管导通，整体对外为低电平。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230501203914339.png" alt="image-20230501203914339" style="zoom: 33%;" />

<p>由于在<strong>开漏输出模式</strong>时，上方的P-MOS管完全不工作，我们可以将其直接忽略掉。如果我们控制输出为0，低电平，则P-MOS管关闭，N-MOS管导通，使输出接地，若控制输出为1 (它无法直接输出高电平)时，则P-MOS管和N-MOS管都关闭，所以引脚既不输出高电平，也不输出低电平，为<strong>高阻态</strong>。为正常使用时必须外部接上拉电阻， 参考下图等效电路中等效电路。它具有“线与”特性，也就是说，若有很多个开漏模式引脚连接到一起时，只有当所有引脚都输出高阻态，才由上拉电阻提供高电平，此高电平的电压为外部上拉电阻所接的电源的电压。若其中一个引脚为低电平，那线路就相当于短路接地，使得整条线路都为低电平，0 V。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image324.png" alt="开漏电路" style="zoom: 67%;" />

<h4 id="1-4-应用"><a href="#1-4-应用" class="headerlink" title="1.4 应用"></a><font size=3>1.4 应用</font></h4><p>推挽输出模式一般应用在输出电平为0和3.3V而且需要高速切换开关状态的场合。在STM32的应用中，除了必须用开漏模式的场合，我们都习惯使用推挽输出模式。开漏输出一般应用在I2C、SMBUS通讯等需要“线与”功能的总线电路中。除此之外，还用在电平不匹配的场合，如需要输出5伏的高电平， 就可以在外部接一个上拉电阻，上拉电源为5伏，并且把GPIO设置为开漏模式，当输出高阻态时，由上拉电阻和电源向外输出5伏的电平，如下图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image419.png" alt="STM32 IO 对外输出 5V电平" style="zoom:50%;" />

<h3 id="1-4-输出数据寄存器"><a href="#1-4-输出数据寄存器" class="headerlink" title="1.4 输出数据寄存器"></a><font size=3>1.4 输出数据寄存器</font></h3><p>前面提到的双MOS管结构电路的输入信号，是由GPIO“输出数据寄存器GPIOx_ODR”提供的，因此我们通过修改输出数据寄存器的值就可以修改GPIO引脚的输出电平。而“置位&#x2F;复位寄存器GPIOx_BSRR”可以通过修改输出数据寄存器④的值从而影响电路的输出。</p>
<p>向该寄存器写入0或者1，可以使普通IO管脚输出高电平或者低电平。</p>
<h3 id="1-5-复用功能输出"><a href="#1-5-复用功能输出" class="headerlink" title="1.5 复用功能输出"></a><font size=3>1.5 复用功能输出</font></h3><p>⑤这个主要是配置GPIO作为除普通IO输出的其他功能电路的输出。</p>
<h3 id="1-6-输入数据寄存器"><a href="#1-6-输入数据寄存器" class="headerlink" title="1.6 输入数据寄存器"></a><font size=3>1.6 输入数据寄存器</font></h3><p>⑥可以读取普通IO管脚输入的高电平或者低电平。</p>
<h3 id="1-7-复用功能输入"><a href="#1-7-复用功能输入" class="headerlink" title="1.7 复用功能输入"></a><font size=3>1.7 复用功能输入</font></h3><p>⑦这个主要是配置GPIO作为除普通IO输入的其他功能电路的输入。</p>
<h3 id="1-8-模拟输入输出"><a href="#1-8-模拟输入输出" class="headerlink" title="1.8 模拟输入输出"></a><font size=3>1.8 模拟输入输出</font></h3><p>⑧为模拟输入，配置此模式时，可以作为ADC或者DAC的管脚。</p>
<h3 id="1-9-TTL肖特基触发器"><a href="#1-9-TTL肖特基触发器" class="headerlink" title="1.9 TTL肖特基触发器"></a><font size=3>1.9 TTL肖特基触发器</font></h3><p>⑨是肖特基触发器，信号经过触发器后，模拟信号转化为0和1的数字信号。但是，当GPIO引脚作为ADC采集电压的输入通道时，用其“模拟输入”功能，此时信号不再经过触发器进行TTL电平转换，这是因为ADC外设要采集到的原始的模拟信号。</p>
<h2 id="2-GPIO的八种工作模式"><a href="#2-GPIO的八种工作模式" class="headerlink" title="2. GPIO的八种工作模式"></a><font size=3>2. GPIO的八种工作模式</font></h2><p>我们从参考手册中可以看到，STM32的GPIO共有八种工作模式</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416173458031.png" alt="image-20230416173458031" style="zoom:50%;" />

<p>这八种工作模式可以分为两类：</p>
<ul>
<li><p>4种输入模式：输入浮空、输入上拉、输入下拉、模拟输入</p>
</li>
<li><p>4种输出模式：开漏输出、开漏复用功能、推挽式输出、推挽式复用功能</p>
</li>
</ul>
<table>
<thead>
<tr>
<th>GPIO八种工作模式</th>
<th>特点及应用</th>
</tr>
</thead>
<tbody><tr>
<td>输入浮空</td>
<td>输入用，完全浮空，状态不定</td>
</tr>
<tr>
<td>输入上拉</td>
<td>输入用，使用内部上拉，默认是高电平</td>
</tr>
<tr>
<td>输入下拉</td>
<td>输入用，使用内部下拉，默认是低电平</td>
</tr>
<tr>
<td>模拟输入</td>
<td>ADC、DAC</td>
</tr>
<tr>
<td>开漏输出</td>
<td>软件模拟I2C的SCL、SDA等</td>
</tr>
<tr>
<td>开漏复用功能</td>
<td>片上外设功能，如硬件I2C的SCL、SDA引脚</td>
</tr>
<tr>
<td>推挽式输出</td>
<td>驱动能力强，25mA（max），作为通用输出</td>
</tr>
<tr>
<td>推挽式复用功能</td>
<td>片上外设功能，如硬件SPI的SCK、MISO、MOSI引脚</td>
</tr>
</tbody></table>
<h3 id="2-1-浮空输入模式"><a href="#2-1-浮空输入模式" class="headerlink" title="2.1 浮空输入模式"></a><font size=3>2.1 浮空输入模式</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416175828910.png" alt="image-20230416175828910" style="zoom: 33%;" />

<p>浮空输入模式，即Floating Input 。两个上&#x2F;下拉电阻开关均断开， 既无上拉也无下拉， I&#x2F;O引脚直接连接TTL肖特基触发器， I&#x2F;O端口的电平信号将会直接进入输入数据寄存器。如果该<strong>引脚悬空，无信号输入时，读取的电平是不确定的</strong>，外部信号是什么电平， MCU引脚就输入什么电平，<strong>I&#x2F;O电平状态是完全由外部输入决定</strong>。 MCU复位上电后，默认为浮空输入模式。  </p>
<h3 id="2-2-上拉输入模式"><a href="#2-2-上拉输入模式" class="headerlink" title="2.2 上拉输入模式"></a><font size=3>2.2 上拉输入模式</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416175929792.png" alt="image-20230416175929792" style="zoom: 33%;" />

<p>上拉输入模式，即Input Pull-up  。VDD经过开关、 上拉电阻，连接外部I&#x2F;O引脚。当开关闭合， 外部I&#x2F;O无输入信号时， 默认输入高电平。该模式的典型应用就是外接按键，当没有按键按下时候， MCU的引脚为确定的高电平，当按键按下时候， 引脚电平被拉为低电平。  </p>
<p>可以看到，输入路径经过施密特触发器后，有两个选择，当端口配置为普通IO输入时，该输入路径进入到输入数据寄存器，因此就可以通过输入数据寄存器读取IO口的电平状态，例如将IO口连接按键，读取外部按键的电平状态。当端口配置成复用功能时，输入路径会直接连接到芯片上具体的外设中去，例如将端口配置成串口USART，那么该IO口就是串口的接收RXD管脚。</p>
<h3 id="2-3-下拉输入模式"><a href="#2-3-下拉输入模式" class="headerlink" title="2.3 下拉输入模式"></a><font size=3>2.3 下拉输入模式</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416175951176.png" alt="image-20230416175951176" style="zoom: 33%;" />

<p>下拉输入模式，即 Input Pull-down 。Vss经过开关、 下拉电阻，连接外部I&#x2F;O引脚。当开关闭合，外部I&#x2F;O无输入信号时，默认输入低电平，I&#x2F;O口读取到的就是低电平。在I&#x2F;O端口输入为高电平的时候，输入端的电平也还是高电平，I&#x2F;O口读取到的电平就是高电平。  </p>
<p>可以看到，输入路径经过施密特触发器后，当有两个选择：端口配置为普通IO输入时，该输入路径进入到输入数据寄存器，因此就可以通过输入数据寄存器读取I&#x2F;O口的电平状态，例如将I&#x2F;O口连接按键，读取外部按键的电平状态。当端口配置成复用功能时，输入路径会直接连接到芯片上具体的外设中去，例如将端口配置成串口USART，那么该IO口就是串口的接收RXD管脚。</p>
<h3 id="2-4-模拟输入模式"><a href="#2-4-模拟输入模式" class="headerlink" title="2.4 模拟输入模式"></a><font size=3>2.4 模拟输入模式</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416180036946.png" alt="image-20230416180036946" style="zoom: 33%;" />

<p>模拟输入模式，即Analog mode 。两个上&#x2F;下拉电阻开关均断开，同时TTL肖特基触发器开关也断开，引脚信号直接连接模拟输入，实现对外部信号的采集。I&#x2F;O端口的模拟信号（电压信号，而非电平信号）直接模拟输入到片上外设模块，比如ADC模块等等。  </p>
<p>GPIO模拟输入，当 STM32需要进行 AD( 模数 ) 转换采样时，需要把引脚设置为模拟输入模式，模拟输入模式下，不需要连接上拉和下拉电阻，因为GPIO用于模拟功能时，引脚的上、下拉电阻是不起作用的。这个时候即使在配置了上拉或下拉电阻，也不会影响到模拟信号的输入。</p>
<p>同时输入信号不再经过TTL施密特触发器，可将外部电压信号直接输入到内部的ADC 外设中去。因为经过施密特触发器后信号只有0、1两种状态，所以ADC外设要采集到原始的、连续变化的模拟信号，信号源输入必须在施密特触发器之前。</p>
<p>需要注意到的是，由于模拟输入不接上下拉电阻，也不经过输入的TTL施密特触发器，同时不像输出功能需要经过输出控制缓冲器，因此在芯片上少连接了很多器件，当芯片有不用的管脚时，可以考虑配置成模拟输入以节省功耗。</p>
<h3 id="2-5-开漏输出模式"><a href="#2-5-开漏输出模式" class="headerlink" title="2.5 开漏输出模式"></a><font size=3>2.5 开漏输出模式</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416180111135.png" alt="image-20230416180111135" style="zoom: 33%;" />

<p>开漏输出，即 Open-Drain， OD  。普通I&#x2F;O设置为开漏输出模式，就是在输出控制时，不使用双MOS电路中的 P-MOS，这样便<strong>无法输出高电平</strong>，只有在输出低电平时，N-MOS生效。开漏输出时，只有N-MOS工作，这样输出数据寄存器可控制I&#x2F;O输出高阻态或低电平。如果要输出高电平，则需要在芯片内部配置上拉电阻（弱上拉）或者在芯片IO外部连接上拉电阻。如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230501203914339.png" alt="image-20230501203914339" style="zoom: 33%;" />

<p>这里要注意N-MOS管，当设置输出的值为高电平的时候，N-MOS管处于关闭状态，此时I&#x2F;O端口的电平就不会由输出的高低电平决定，而是由I&#x2F;O端口外部的上拉或者下拉决定。当设置输出的值为低电平的时候，N-MOS管处于开启状态，此时I&#x2F;O端口的电平就是低电平。同时，I&#x2F;O端口的电平也可以通过输入电路进行读取；<strong>注意</strong>，I&#x2F;O端口的电平不一定是输出的电平，另外此时对输入数据寄存器的读访问可得到I&#x2F;O状态。</p>
<p>使用开漏输出时，可以实现以下功能：</p>
<p>（1）提高IO驱动能力。MCU的IO口对外驱动的能力很小，即使使用芯片内部的上拉电阻，由于内部上拉阻值很大，驱动能力也有限。因此使用开漏输出时，可以在IO口外部连接上拉电阻，此时驱动电流芯片由外部上拉电阻电源提供，这样提高驱动和带负载能力。</p>
<p>（2）实现电平转换。如果MCU只能对外输出3.3v，但由于GPIO管脚是5V兼容的，输出高电平可以通过外部电源提供，外部电源如果是5V。此时可以对外输出5V。</p>
<p>（3）实现“逻辑与”关系。多个开漏的引脚可以直接并在一起使用，统一接一个合适的上拉电阻，就可以实现“逻辑与”关系，即当所有引脚均输出高电平时，输出才为高电平，若任一引脚输出低电平，则输出低电平。在I2C、 SMBUS等总线电路中经常会用到。  </p>
<h3 id="2-6-复用功能开漏输出模式"><a href="#2-6-复用功能开漏输出模式" class="headerlink" title="2.6 复用功能开漏输出模式"></a><font size=3>2.6 复用功能开漏输出模式</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416180131497.png" alt="image-20230416180131497" style="zoom: 33%;" />

<p>复用功能，即 Alternate Function， AF 。这一小节是复用功能开漏输出模式，与开漏输出模式很是类似。只是输出的高低电平的来源，不是让CPU直接写输出数据寄存器，而是通过片上外设模块的复用功能输出来决定。</p>
<p>复用功能开漏输出时，GPIO就不会经过输出数据寄存器输出，而是直接通过芯片上的具体外设功能电路输出到输出控制缓冲器。此时通过读输入数据寄存器可获取I&#x2F;O实际状态，但一般直接用外设的寄存器来获取该数据信号。</p>
<p>复用功能开漏输出的功能和普通IO开漏输出功能一致，因此GPIO用作复用功能管脚如果要输出高电平，则需要在芯片内部配置上拉电阻（弱上拉）或者在芯片IO外部连接上拉电阻。例如将GPIO配置成I2C信号线的时钟管脚SCL和数据管脚SDA。</p>
<h3 id="2-7-推挽输出模式"><a href="#2-7-推挽输出模式" class="headerlink" title="2.7 推挽输出模式"></a><font size=3>2.7 推挽输出模式</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230506192959951.png" alt="image-20230506192959951" style="zoom:33%;" />

<p>推挽输出模式，即Open-Drain， OD 。推挽输出模式下，通过设置位设置&#x2F;清除寄存器或者输出数据寄存器的值，途经P-MOS管和N-MOS管，最终输出到I&#x2F;O端口。这里要注意P-MOS管和N-MOS管，输出寄存器上的’0’激活N-MOS，而输出寄存器上的’1’将激活P-MOS，即：</p>
<ul>
<li><p>当设置输出的值为高电平（1）的时候，P-MOS管处于开启状态，N-MOS管处于关闭状态，电流方向为VDD&rarr;P-MOS&rarr;输出端，对外部引脚输出高电平；</p>
</li>
<li><p>当设置输出的值为低电平（0）的时候，P-MOS管处于关闭状态，N-MOS管处于开启状态，电流方向为VSS&rarr;N-MOS&rarr;输出端，对外输出低电平。</p>
</li>
</ul>
<p>同时，这种模式下，I&#x2F;O端口的电平也可以通过输入电路进行读取；注意，此时I&#x2F;O端口的电平一定是输出的电平。</p>
<h3 id="2-8-复用功能推挽输出模式"><a href="#2-8-复用功能推挽输出模式" class="headerlink" title="2.8 复用功能推挽输出模式"></a><font size=3>2.8 复用功能推挽输出模式</font></h3><img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416180215294.png" alt="image-20230416180215294" style="zoom: 33%;" />

<p>复用功能，即 Alternate Function， AF 。这一小节是复用功能推挽输出模式，与推挽输出模式很是类似。只是输出的高低电平的来源，不是让CPU直接写输出数据寄存器，而是通过片上外设模块的复用功能输出来决定。</p>
<p>GPIO不仅仅可以用作普通的 IO 口输出，还可以作为芯片上其他外设的特殊功能引脚，有些引脚可能可以用作多种不同功能，这种就叫做GPIO的复用，具体用作哪种功能，需要根据使用进行相应的配置，后边会详细再说。使用成复用功能时，GPIO就不会经过输出数据寄存器输出，而是直接通过芯片上的具体外设功能电路输出到输出控制缓冲器。此时通过读输入数据寄存器可获取I&#x2F;O实际状态，但一般直接用外设的寄存器来获取该数据信号。</p>
<p>使用GPIO复用功能时，当配置成推挽输出，这样片上外设就可以输出高电平或者低电平，例如将GPIO配置成串口USART输出TXD管脚。</p>
<p>需要注意的是，如果需要实现DAC模拟输出，那么也需要将管脚配置成复用功能，但是此时作为“模拟输出”功能，DAC的模拟信号输出就不经过双MOS管结构了，在GPIO结构框图的右下角处，模拟信号直接输出到引脚。</p>
<p>用于复用功能时，可使用上拉、下拉或者浮空模式。在这种情况下，初始化后引脚默认输出低电平，上拉只起到小幅提高输出电流能力，并不会影响引脚的默认状态。具体要配置成什么样子，可以查看《STM32中文参考手册》的8.1.11 外设的GPIO配置 。  </p>
<h2 id="3-GPIO-输出速度"><a href="#3-GPIO-输出速度" class="headerlink" title="3. GPIO 输出速度  "></a><font size=3>3. GPIO 输出速度  </font></h2><p>STM32的I&#x2F;O引脚工作在输出模式下时， 需要配置I&#x2F;O引脚的输出速度。该输出速度不是输出信号的速度，而是I&#x2F;O口驱动电路的响应速度。STM32提供三个输出速度： 2MHz、 10MHz、 50MHz。 实际开发中需要结合实际情况选择合适的相应速度，以兼顾信号的稳定性和低功耗。 通常，当设置为高速时， 功耗高、噪声大、电磁干扰强；当设备为低速时， 功耗低、噪声小、电磁干扰弱。</p>
<p>通常简单外设，比如LED灯、蜂鸣器灯，建议使用2MHz的输出速度， 而复用为I2C、 SPI等通信信号引脚时，建议使用10MHz或50MHz以提高响应速度。  </p>
<h2 id="4-几个问题"><a href="#4-几个问题" class="headerlink" title="4. 几个问题"></a><font size=3>4. 几个问题</font></h2><h3 id="4-1-什么是推挽结构和推挽电路"><a href="#4-1-什么是推挽结构和推挽电路" class="headerlink" title="4.1 什么是推挽结构和推挽电路"></a><font size=3>4.1 什么是推挽结构和推挽电路</font></h3><p><strong>推挽结构</strong>一般是指两个参数相同的三极管或MOS管分别受两互补信号的控制，总是在一个三极管或MOS管导通的时候另一个截止。高低电平由输出电平决定。</p>
<p><strong>推挽电路</strong>是两个参数相同的三极管或MOSFET，以推挽方式存在于电路中，各负责正负半周的波形放大任务。电路工作时，两只对称的功率开关管每次只有一个导通，所以导通损耗小、效率高。输出既可以向负载灌电流，也可以从负载抽取电流。推拉式输出级既提高电路的负载能力，又提高开关速度。</p>
<h3 id="4-2-开漏输出和推挽输出的区别"><a href="#4-2-开漏输出和推挽输出的区别" class="headerlink" title="4.2 开漏输出和推挽输出的区别"></a><font size=3>4.2 开漏输出和推挽输出的区别</font></h3><ul>
<li>开漏输出：只可以输出强低电平，高电平得靠外部电阻拉高。输出端相当于三极管的集电极。适合于做电流型的驱动，其吸收电流的能力相对强(一般20mA以内)。</li>
<li>推挽输出:可以输出强高、低电平，连接数字器件。</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/20180409133536882.jpeg" alt="img" style="zoom:80%;" />

<p>左边的便是推挽输出模式，其中比较器输出高电平时下面的PNP三极管截止，而上面NPN三极管导通，输出电平VS+；当比较器输出低电平时则恰恰相反，PNP三极管导通，输出和地相连，为低电平。</p>
<p>右边的则可以理解为开漏输出形式，需要接上拉。</p>
<h1 id="二、寄存器说明"><a href="#二、寄存器说明" class="headerlink" title="二、寄存器说明"></a><font size=3>二、寄存器说明</font></h1><p>STM32的每个 IO 口可以自由编程， 但 <strong>IO 口寄存器必须要按 32 位字被访问（不允许半字或字节访问）</strong>。 STM32 的很多 IO 口都是 5V 兼容的，这些 IO 口在与 5V 电平的外设连接的时候很有优势，具体哪些 IO 口是 5V 兼容的，可以从该芯片的数据手册管脚描述章节查到（I&#x2F;O Level 标 FT 的就是 5V 电平兼容的）。  </p>
<p>STM32 的每个 IO 端口都有 7 个寄存器来控制。他们分别是：</p>
<ul>
<li>2 个 32 位的配置模式的端口配置寄存器 CRL 和 CRH； </li>
<li>2 个 32 位的数据寄存器 IDR 和 ODR； </li>
<li>1 个 32 位的置位&#x2F;复位寄存器BSRR；</li>
<li>1个 16 位的复位寄存器 BRR； </li>
<li>1 个 32 位的锁存寄存器 LCKR；</li>
</ul>
<p>这里就不详细说明寄存器了，STM32有中文参考手册，所以这些我们可以去看中文参考手册，里边有详细说明。</p>
<h1 id="三、STM32端口复用和重映射"><a href="#三、STM32端口复用和重映射" class="headerlink" title="三、STM32端口复用和重映射"></a><font size=3>三、STM32端口复用和重映射</font></h1><h2 id="1-端口复用"><a href="#1-端口复用" class="headerlink" title="1. 端口复用"></a><font size=3>1. 端口复用</font></h2><h3 id="1-1-基本概念"><a href="#1-1-基本概念" class="headerlink" title="1.1 基本概念"></a><font size=3>1.1 基本概念</font></h3><p>STM32有很多的内置外设，这些外设的外部引脚都是与GPIO复用的。也就是说，一个GPIO如果可以复用为内置外设的功能引脚，那么当这个GPIO作为内置外设使用的时候，就叫做复用。</p>
<p>其实所谓复用，就是一些端口不仅仅可以做为通用IO口，还可以复用为一些外设引脚，例如串口1 的发送接收引脚是PA9、PA10，当我们把PA9、PA10不用作GPIO，而用做复用功能串口1的发送接收引脚的时候，叫端口复用。这样做的作用就是可以最大限度的利用端口资源。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416190146605.png" alt="image-20230416190146605" style="zoom: 25%;" />

<h3 id="1-2-端口复用配置步骤"><a href="#1-2-端口复用配置步骤" class="headerlink" title="1.2 端口复用配置步骤"></a><font size=3>1.2 端口复用配置步骤</font></h3><p>以PA9、PA10配置为串口1为例（到学习串口的时候会再说）：</p>
<p>（1）lGPIO端口时钟使能</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);</span><br></pre></td></tr></table></figure>

<p>（2）复用外设时钟使能</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 比如你要将端口PA9,PA10复用为串口，所以要使能串口时钟</span></span><br><span class="line">RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);</span><br></pre></td></tr></table></figure>

<p>（3）端口模式配置，主要是初始化引脚功能</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GPIO_Init()</span><br></pre></td></tr></table></figure>

<p>这个需要查《STM32中文参考手册V10》P110的表格 “8.1.11外设的GPIO配置”。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191434797.png" alt="image-20230416191434797" style="zoom:45%;" />

<h3 id="1-3-资料查找"><a href="#1-3-资料查找" class="headerlink" title="1.3 资料查找"></a><font size=3>1.3 资料查找</font></h3><p>我上哪找这个端口可以服用为什么功能？当然还是中文参考手册和数据手册啦。</p>
<table>
    <tr><td align="left"><a href="https://www.st.com/resource/en/datasheet/stm32f103ze.pdf" target="_blank">STM32F103xx数据手册</a></td><td align="left">STM32F103xx英文数据手册</td></tr>
    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/709978" target="_blank">STM32F103xx中文数据手册</a></td><td align="left">STM32F103xx中文数据手册</td></tr>
    <tr><td align="left"><a href="https://www.stmcu.com.cn/Designresource/detail/localization_document%20/710001" target="_blank">STM32F10xxx参考手册（RM0008）</a></td><td align="left">STM32F10xxx单片机的中文参考手册</td></tr>
    <tr><td align="left"><a href="https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf" target="_blank">STM32F10xxx参考手册（RM0008）</a></td><td align="left">STM32F10xxx单片机的英文参考手册</td></tr>
</table>




<ul>
<li>参考手册——8.1.11 外设的GPIO配置</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191434797.png" alt="image-20230416191434797" style="zoom:45%;" />

<ul>
<li>参考手册——8.3 复用功能I&#x2F;O和调试配置(AFIO)</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191400174.png" alt="image-20230416191400174" style="zoom:50%;" />

<ul>
<li>数据手册——3 Pinouts and pin descriptions</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416190146605.png" alt="image-20230416190146605" style="zoom: 25%;" />

<h2 id="2-端口重映射"><a href="#2-端口重映射" class="headerlink" title="2. 端口重映射"></a><font size=3>2. 端口重映射</font></h2><h3 id="2-1-基本概念"><a href="#2-1-基本概念" class="headerlink" title="2.1 基本概念"></a><font size=3>2.1 基本概念</font></h3><p>每个内置外设都有若干个输入输出引脚，一般这些引脚的输出端口都是固定不变的，为了让设计工程师可以更好地安排引脚的走向和功能，在STM32中引入了外设引脚重映射的概念，即一个外设的引脚除了具有默认的端口外，还可以通过设置重映射寄存器的方式，把这个外设的引脚映射到其它的端口。</p>
<p>为了使不同器件封装的外设IO功能数量达到最优，可以把一些复用功能重新映射到其他一些引脚上。STM32中有很多内置外设的输入输出引脚都具有重映射(remap)的功能。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230506192914603.png" alt="image-20230506192914603" style="zoom:33%;" />

<h3 id="2-2-部分重映射-完全重映射"><a href="#2-2-部分重映射-完全重映射" class="headerlink" title="2.2 部分重映射 &amp; 完全重映射"></a><font size=3>2.2 部分重映射 &amp; 完全重映射</font></h3><ul>
<li><p>部分重映射：功能外设的部分引脚重新映射，还有一部分引脚是原来的默认引脚。</p>
</li>
<li><p>完全重映射：功能外设的所有引脚都重新映射。</p>
</li>
</ul>
<p>例如，在STM32中，USART3支持部分重映像（STM32中文参考手册的8.3.8 USART复用功能重映射 ）：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416214223104.png" alt="image-20230416214223104" style="zoom: 33%;" />

<h3 id="2-3-重映射步骤"><a href="#2-3-重映射步骤" class="headerlink" title="2.3 重映射步骤"></a><font size=3>2.3 重映射步骤</font></h3><p>以PA9、PA10配置为串口1为例：</p>
<p>（1）使能<strong>被重新映射到的</strong>GPIO端口时钟；</p>
<p>（2）使能被重新映射的外设时钟；</p>
<p>（3）使能AFIO功能的时钟，<strong>重映射必须使能AFIO时钟</strong>；</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);</span><br></pre></td></tr></table></figure>

<p>（4）进行重映射</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 根据第一个参数，来确定是部分重映射还是全部重映射</span></span><br><span class="line">GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>哪些情况需要开启AFIO辅助功能时钟？</p>
<p>对寄存器AFIO_MAPR，AFIO_EXTICRX和AFIO_EVCR进行读写操作前，应当首先打开AFIO时钟。</p>
<ul>
<li>AFIO_MAPR：配置复用功能重映射</li>
<li>AFIO_EXTICRX:配置外部中断线映射</li>
<li>AFIO_EVCR:  配置EVENTOUT事件输出</li>
</ul>
</blockquote>
<h3 id="2-4-资料查找"><a href="#2-4-资料查找" class="headerlink" title="2.4 资料查找"></a><font size=3>2.4 资料查找</font></h3><p>去哪找资料？比如串口1默认引脚是PA9,PA10可以通过配置重映射映射到PB6、PB7，我们可以找的方向如下：</p>
<ul>
<li>参考手册——8.3 复用功能I&#x2F;O和调试配置(AFIO)</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191400174.png" alt="image-20230416191400174" style="zoom:50%;" />

<ul>
<li>数据手册——3 Pinouts and pin descriptions</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230416191130706.png" alt="image-20230416191130706" style="zoom:37%;" />

<h1 id="四、位带操作"><a href="#四、位带操作" class="headerlink" title="四、位带操作"></a><font size=3>四、位带操作</font></h1><h2 id="1-相关知识回顾"><a href="#1-相关知识回顾" class="headerlink" title="1. 相关知识回顾"></a><font size=3>1. 相关知识回顾</font></h2><p>在笔记《LV16-10-存储器系统-01-CortexM3》中我们详细了解看了位带操作，我们知道在Cortex-M3的SRAM和外设存储区会有位带别名(alias)区和位带区，而STM32是基于Cortex-M3内核的，具体的存储器映射是由芯片厂家进行设置，但是也都是遵循Cotex-M3的映射方式，我们来看一下[STM32中文参考手册](<a target="_blank" rel="noopener" href="https://www.stmcu.com.cn/Designresource/detail/localization_document">https://www.stmcu.com.cn/Designresource/detail/localization_document</a> &#x2F;710001)的2.3.2 位段一节：</p>
<p>Cortex™-M3存储器映像包括两个位段(bit-band)区。这两个位段区将别名存储器区中的每个字 映射到位段存储器区的一个位，在别名存储区写入一个字具有对位段区的目标位执行读-改-写操 作的相同效果。 在STM32F10xxx里，外设寄存器和SRAM都被映射到一个位段区里，这允许执行单一的位段的 写和读操作。 下面的映射公式给出了别名区中的每个字是如何对应位带区的相应位的：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bit_word_addr = bit_band_base + (byte_offset×<span class="number">32</span>) + (bit_number×<span class="number">4</span>)</span><br></pre></td></tr></table></figure>

<p>其中： bit_word_addr是别名存储器区中字的地址，它映射到某个目标位。 bit_band_base是别名区的起始地址。 byte_offset是包含目标位的字节在位段里的序号 bit_number是目标位所在位置(0-31)  </p>
<p>例子： 下面的例子说明如何映射别名区中SRAM地址为0x20000300的字节中的位2：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">0x22006008</span> = <span class="number">0x22000000</span> + (<span class="number">0x300</span>×<span class="number">32</span>) + (<span class="number">2</span>×<span class="number">4</span>)</span><br></pre></td></tr></table></figure>

<p>对0x22006008地址的写操作与对SRAM中地址0x20000300字节的位2执行读-改-写操作有着相同的效果。</p>
<p>我看了下STM32的数据手册和中文参考手册，存储器映像中只体现出来了SRAM的位带区和位带别名区，外设区的没体现，不过我们依然可以用，我们可以参考Cortex-M3的：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230506213749244.png" alt="image-20230506213749244" style="zoom:50%;" />

<h2 id="2-相关宏定义"><a href="#2-相关宏定义" class="headerlink" title="2. 相关宏定义"></a><font size=3>2. 相关宏定义</font></h2><h3 id="2-1-宏"><a href="#2-1-宏" class="headerlink" title="2.1 宏"></a><font size=3>2.1 宏</font></h3><p>当我们配置好GPIO口之后，要想控制某一个管脚输出高低电平的话，需要使用函数，不免有些麻烦，直接操作寄存器实际会快的多，这里进行宏定义，实际上是直接操作寄存器，直接向GPIO的数据寄存器写入数据，下边的定义是基于HAL库的，对应不同的库的话，只需要更改为对应库中各个端口的基址寄存器即可。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;stm32f1xx.h&quot;</span></span></span><br><span class="line"><span class="comment">//位带操作,实现51类似的GPIO控制功能 具体实现思想,参考&lt;&lt;CM3权威指南&gt;&gt;第五章(87页~92页).</span></span><br><span class="line"><span class="comment">//IO口操作宏定义</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BITBAND(addr, bitnum) ((addr &amp; 0xF0000000)+0x2000000+((addr &amp; 0xFFFFF)&lt;&lt;5)+(bitnum&lt;&lt;2)) </span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MEM_ADDR(addr)  *((volatile unsigned long  *)(addr)) </span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> BIT_ADDR(addr, bitnum)   MEM_ADDR(BITBAND(addr, bitnum)) </span></span><br><span class="line"><span class="comment">//IO口地址映射</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOA_ODR_Addr    (GPIOA_BASE+12) <span class="comment">//0x4001080C </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOB_ODR_Addr    (GPIOB_BASE+12) <span class="comment">//0x40010C0C </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOC_ODR_Addr    (GPIOC_BASE+12) <span class="comment">//0x4001100C </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOD_ODR_Addr    (GPIOD_BASE+12) <span class="comment">//0x4001140C </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOE_ODR_Addr    (GPIOE_BASE+12) <span class="comment">//0x4001180C </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOF_ODR_Addr    (GPIOF_BASE+12) <span class="comment">//0x40011A0C    </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOG_ODR_Addr    (GPIOG_BASE+12) <span class="comment">//0x40011E0C    </span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOA_IDR_Addr    (GPIOA_BASE+8) <span class="comment">//0x40010808 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOB_IDR_Addr    (GPIOB_BASE+8) <span class="comment">//0x40010C08 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOC_IDR_Addr    (GPIOC_BASE+8) <span class="comment">//0x40011008 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOD_IDR_Addr    (GPIOD_BASE+8) <span class="comment">//0x40011408 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOE_IDR_Addr    (GPIOE_BASE+8) <span class="comment">//0x40011808 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOF_IDR_Addr    (GPIOF_BASE+8) <span class="comment">//0x40011A08 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOG_IDR_Addr    (GPIOG_BASE+8) <span class="comment">//0x40011E08 </span></span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//IO口操作,只对单一的IO口! 确保n的值小于16!</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  <span class="comment">//输出 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PAin(n)    BIT_ADDR(GPIOA_IDR_Addr,n)  <span class="comment">//输入 </span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PBout(n)   BIT_ADDR(GPIOB_ODR_Addr,n)  <span class="comment">//输出 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PBin(n)    BIT_ADDR(GPIOB_IDR_Addr,n)  <span class="comment">//输入 </span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PCout(n)   BIT_ADDR(GPIOC_ODR_Addr,n)  <span class="comment">//输出 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PCin(n)    BIT_ADDR(GPIOC_IDR_Addr,n)  <span class="comment">//输入 </span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PDout(n)   BIT_ADDR(GPIOD_ODR_Addr,n)  <span class="comment">//输出 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PDin(n)    BIT_ADDR(GPIOD_IDR_Addr,n)  <span class="comment">//输入 </span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PEout(n)   BIT_ADDR(GPIOE_ODR_Addr,n)  <span class="comment">//输出 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PEin(n)    BIT_ADDR(GPIOE_IDR_Addr,n)  <span class="comment">//输入</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PFout(n)   BIT_ADDR(GPIOF_ODR_Addr,n)  <span class="comment">//输出 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PFin(n)    BIT_ADDR(GPIOF_IDR_Addr,n)  <span class="comment">//输入</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PGout(n)   BIT_ADDR(GPIOG_ODR_Addr,n)  <span class="comment">//输出 </span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> PGin(n)    BIT_ADDR(GPIOG_IDR_Addr,n)  <span class="comment">//输入</span></span></span><br></pre></td></tr></table></figure>

<h3 id="2-2-分析"><a href="#2-2-分析" class="headerlink" title="2.2 分析"></a><font size=3>2.2 分析</font></h3><p>我们拿 PAout(2) 来分析一下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> PAout(n)   BIT_ADDR(GPIOA_ODR_Addr,n)  <span class="comment">//输出</span></span></span><br></pre></td></tr></table></figure>

<ul>
<li>（1）我们先找到 GPIOA_ODR_Addr 是多少：</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> PERIPH_BASE           0x40000000UL <span class="comment">/*!&lt; Peripheral base address in the alias region */</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> APB1PERIPH_BASE       PERIPH_BASE</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> APB2PERIPH_BASE       (PERIPH_BASE + 0x00010000UL)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOA_BASE            (APB2PERIPH_BASE + 0x00000800UL)</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> GPIOA_ODR_Addr        (GPIOA_BASE+12) <span class="comment">//0x4001080C </span></span></span><br></pre></td></tr></table></figure>

<p>从这些宏，我们知道：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">GPIOA_ODR_Addr = GPIOA_BASE+<span class="number">12</span></span><br><span class="line">               = APB2PERIPH_BASE + <span class="number">0x00000800</span> + <span class="number">12</span></span><br><span class="line">               = PERIPH_BASE + <span class="number">0x00010000</span> + <span class="number">0x00000800</span> + <span class="number">12</span></span><br><span class="line">    		   = <span class="number">0x40000000</span> + <span class="number">0x00010000</span> + <span class="number">0x00000800</span> + <span class="number">12</span></span><br><span class="line">    		   = <span class="number">0x40010000</span> + <span class="number">0x00000800</span> + <span class="number">12</span></span><br><span class="line">    	       = <span class="number">0x40010800</span> + <span class="number">0x0C</span></span><br></pre></td></tr></table></figure>

<p>我们看一下ODR寄存器地址：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV16-STM32%E5%BC%80%E5%8F%91/LV16-12-GPIO-01-STM32%E7%9A%84GPIO/img/image-20230506214456939.png" alt="image-20230506214456939" style="zoom: 67%;" />

<p>所以 GPIOA_ODR_Addr 实际上就代表了 GPIOA_ODR寄存器的地址。</p>
<ul>
<li>（2）位带别名区地址分析</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> BITBAND(addr, bitnum) ((addr &amp; 0xF0000000)+0x2000000+((addr &amp;0xFFFFF)&lt;&lt;5)+(bitnum&lt;&lt;2)) </span></span><br><span class="line"></span><br><span class="line">bitnum = <span class="number">2</span>;</span><br><span class="line">addr= <span class="number">0x40010800</span> + <span class="number">0x0C</span> = <span class="number">0x4001080C</span></span><br></pre></td></tr></table></figure>

<p>所以：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">BITBAND(addr, bitnum) = BITBAND(<span class="number">0x4001080C</span>, <span class="number">2</span>)</span><br><span class="line">    				  = (<span class="number">0x4001080C</span> &amp; <span class="number">0xF0000000</span>) + <span class="number">0x2000000</span> + ((<span class="number">0x4001080C</span> &amp; <span class="number">0xFFFFF</span>)&lt;&lt;<span class="number">5</span>) + (<span class="number">2</span>&lt;&lt;<span class="number">2</span>)</span><br><span class="line">                      = <span class="number">0x40000000</span> + <span class="number">0x2000000</span> + (<span class="number">0x1080C</span>) * <span class="number">32</span> + <span class="number">2</span> * <span class="number">4</span></span><br><span class="line">    				  = <span class="number">0x42000000</span> + (<span class="number">0x1080C</span>) * <span class="number">32</span> + <span class="number">2</span> * <span class="number">4</span></span><br><span class="line">                      = <span class="number">0x42000000</span> + <span class="number">0x00210180</span> + <span class="number">8</span></span><br><span class="line">                      = <span class="number">0x42210188</span></span><br></pre></td></tr></table></figure>

<ul>
<li>（3）读写分析</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">PAout(<span class="number">2</span>) = BIT_ADDR(<span class="number">0x4001080C</span>, <span class="number">2</span>)</span><br><span class="line">         = MEM_ADDR(BITBAND(<span class="number">0x4001080C</span>, <span class="number">2</span>))</span><br><span class="line">         = MEM_ADDR(<span class="number">0x42210188</span>)</span><br><span class="line">         = *((<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">long</span>  *)(<span class="number">0x42210188</span>))</span><br></pre></td></tr></table></figure>

<p>如此我们便可以直接向某一位写0或者1了。</p>
<h2 id="3-使用实例"><a href="#3-使用实例" class="headerlink" title="3. 使用实例"></a><font size=3>3. 使用实例</font></h2><p>假设已经初始化好了GPIOE5为推挽输出，那么：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">PEout(<span class="number">5</span>) = <span class="number">1</span>; <span class="comment">// GPIOE_PIN5 会直接输出高电平</span></span><br><span class="line">PEout(<span class="number">5</span>) = <span class="number">0</span>; <span class="comment">// GPIOE_PIN5 会直接输出低电平</span></span><br></pre></td></tr></table></figure>

<h1 id="五、HAL库函数分析"><a href="#五、HAL库函数分析" class="headerlink" title="五、HAL库函数分析"></a><font size=3>五、HAL库函数分析</font></h1><h2 id="1-GPIO-InitTypeDef"><a href="#1-GPIO-InitTypeDef" class="headerlink" title="1. GPIO_InitTypeDef"></a><font size=3>1. GPIO_InitTypeDef</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> <span class="class"><span class="keyword">struct</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">  <span class="type">uint32_t</span> Pin;     <span class="comment">// 控制的 GPIO 引脚</span></span><br><span class="line">  <span class="type">uint32_t</span> Mode;    <span class="comment">// GPIO输出类型</span></span><br><span class="line">  <span class="type">uint32_t</span> Pull;    <span class="comment">// GPIO上下拉配置</span></span><br><span class="line">  <span class="type">uint32_t</span> Speed;   <span class="comment">// GPIO速率</span></span><br><span class="line">&#125; GPIO_InitTypeDef;</span><br></pre></td></tr></table></figure>

<h2 id="2-读写相关函数"><a href="#2-读写相关函数" class="headerlink" title="2. 读写相关函数"></a><font size=3>2. 读写相关函数</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">GPIO_PinState <span class="title function_">HAL_GPIO_ReadPin</span><span class="params">(GPIO_TypeDef *GPIOx, <span class="type">uint16_t</span> GPIO_Pin)</span>;</span><br><span class="line"><span class="type">void</span> <span class="title function_">HAL_GPIO_WritePin</span><span class="params">(GPIO_TypeDef *GPIOx, <span class="type">uint16_t</span> GPIO_Pin, GPIO_PinState PinState)</span>;</span><br><span class="line"><span class="type">void</span> <span class="title function_">HAL_GPIO_TogglePin</span><span class="params">(GPIO_TypeDef *GPIOx, <span class="type">uint16_t</span> GPIO_Pin)</span>;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/63f07cb0.html">LV16-12-GPIO-01-STM32的GPIO</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年06月04日 - 22:06</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/63f07cb0.html" title="LV16-12-GPIO-01-STM32的GPIO">https://sumumm.github.io/post/63f07cb0.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV16-STM32%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV16-STM32开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/7ab994a2.html" rel="prev" title="LV16-12-GPIO-02-输入输出模式详解">
                  <i class="fa fa-angle-left"></i> LV16-12-GPIO-02-输入输出模式详解
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/b1786358.html" rel="next" title="LV16-11-STM32启动-03-在内部SRAM中调试代码">
                  LV16-11-STM32启动-03-在内部SRAM中调试代码 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
